﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sales_Sales : ATPages
{
    Function f = new Function();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            txtSdate.Text = DateTime.Now.ToShortDateString();
            TextBox3.Text = DateTime.Now.ToShortDateString();

            Hidid.Value =Server.UrlDecode(Request.Cookies["user"].Values["id"]);
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            initData();
            initDDL();
            lblDh.Text = initBarCode();
            hidsj.Value = initBarbs();
            Hidsj2.Value = hidsj.Value;
           // initGifts();
            initGvData();
            btnAdd.Attributes.Add("onclick", "return checkSale(this.form)");
            btnAd.Attributes.Add("onclick", "return check(this.form)");
        }
    }
   

    protected void linkbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("SalesManage.aspx?moduid=" + Request["moduid"]);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ZSql edit = new ZSql();

        ZSql sales = new ZSql();
        ZSql update = new ZSql();
        ZSql dele = new ZSql();
        ZSql mail = new ZSql();
        ZSql zs = new ZSql();
        string guestid = zs.GetScalar("insert into CS_Guest (GuestName,GuestMan,Phone,Address,EditUserName,AuditState,EditDate)values('" + Common.FormatParameter(txtGuest.Text) + "','" + Common.FormatParameter(txtGuestName.Text) + "','" + Common.FormatParameter(txtTel.Text) + "','" + Common.FormatParameter(txtAddress.Text) + "','" + lblName.Text + "','已通过','" + txtSdate.Text + "');select nid = @@identity").ToString();
        zs.Close();
        dele.Open("select * from CS_ls where LS_dh='" + hidsj.Value + "'");
        string dcode = initBarCode();
        //int num = 0;
        for (int i = 0; i < dele.m_table.Rows.Count; i++)
        {

            string sql = "insert into CS_Sales(SalesCode,GuestId,C_date,comodBarcode,Brand,comodName,modul,Unit,Supplier,Prices,number,userid,SalesContent,DeliveryDate,Saleman,ProcMoney,WarrTime,TotalMoney,practical,TotalPractical,typeSale,deptid,typeStock,WarehouseId)values('" + dcode + "','" + guestid + "','" + txtSdate.Text + "','" + dele.m_table.Rows[i]["LS_BarCode"].ToString() + "','" + dele.m_table.Rows[i]["LS_band"].ToString() + "','" + dele.m_table.Rows[i]["LS_name"].ToString() + "','" + dele.m_table.Rows[i]["LS_modul"].ToString() + "','" + dele.m_table.Rows[i]["LS_unit"].ToString() + "','" + dele.m_table.Rows[i]["LS_Supplier"].ToString() + "'," + dele.m_table.Rows[i]["LS_price"].ToString() + "," + dele.m_table.Rows[i]["LS_num"].ToString() + "," + Request.Cookies["user"].Values["id"] + ",'" + Common.FormatParameter(txtContent.Text) + "','" + TextBox3.Text + "'," + dele.m_table.Rows[i]["LS_Saleman"].ToString() + "," + dele.m_table.Rows[i]["LS_ProcMoney"].ToString() + "," + dele.m_table.Rows[i]["LS_Warr"].ToString() + "," + dele.m_table.Rows[i]["LS_TotalMoney"].ToString() + "," + dele.m_table.Rows[i]["LS_practical"].ToString() + "," + dele.m_table.Rows[i]["LS_TotalPractical"].ToString() + ",1," + Request.Cookies["user"].Values["deptid"] + ",'" + dele.m_table.Rows[i]["LS_typeStock"].ToString() + "','" + HFCKID.Value + "')";
            //string sql1 = "insert into CS_Mailadd(SalesCode,Guestname,Guestmen,Guesttel,Guestaddress,username,dispTime,comidunit,cominame,comitype,comidband,comidSpe,issale,typeSale)values('" + dele.m_table.Rows[i]["LS_BarCode"].ToString() + "','" + Common.FormatParameter(txtGuest.Text) + "','" + Common.FormatParameter(txtGuestName.Text) + "','" + Common.FormatParameter(txtTel.Text) + "','" + Common.FormatParameter(txtAddress.Text) + "','" + lblName.Text + "','" + txtSdate.Text + "','" + dele.m_table.Rows[i]["LS_unit"].ToString() + "','" + dele.m_table.Rows[i]["LS_name"].ToString() + "','" + dele.m_table.Rows[i]["LS_type"].ToString() + "','" + dele.m_table.Rows[i]["LS_band"].ToString() + "','" + dele.m_table.Rows[i]["LS_modul"].ToString() + "',1,1)";
            //mail.Open(sql1);
            sales.Open(sql);
        }
        dele.Open("delete CS_ls where LS_dh='" + hidsj.Value + "'");//清除临时销售表
        clearData();
     
        edit.Close();
        sales.Close();
        update.Close();

        lblHnum.Text = "0";
        lblHMoney.Text = "0";
        lblXmoney.Text = "0";
        lblXnum.Text = "0";
        HidCountNum.Value = "0";

        initGvData();
        cler();
        if (isprint.Checked)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + dcode + "')", true);
        }
        //Response.Redirect("Sales.aspx?moduid=" + Request["moduid"]);
    }

    private void cler()
    {
        txtGuest.Text = "";
        txtGuestName.Text = "";
        txtTel.Text = "";
        txtAddress.Text = "";
        ddlUser.SelectedIndex = 0;
        txtContent.Text = "";
    }

    private void initData()
    {
        ZSql zs = new ZSql();

        if (Hidware.Value != "0")
        {
            zs.Open("select a.content,producer,salesprices ,stockid,WarehouseName,a.WarehouseId,e.Brand,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse h on a.warehouseid=h.warehouseid where  a.comodid='" + Request["id"] + "'");
            lblName.Text = lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
        }
        else
        {
            zs.Open("select a.content,producer,salesprices ,stockid,WarehouseName,a.WarehouseId,e.Brand,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse h on a.warehouseid=h.warehouseid where  a.comodid='" + Request["id"] + "'");

            lblName.Text = lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
        }
        zs.Close();
    }

    private string initBarCode()//销售正式流水号
    {
        ZSql zs = new ZSql();
        string id = "XS" + DateTime.Now.ToString("yyyyMMdd");
        zs.Open("select max(SalesId) as a from CS_Sales");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }
        return id;
    }


    private string initBarbs()//销售临时流水号
    {
        string a = "ls";
        Random r = new Random();
        a += DateTime.Now.Second.ToString() + r.Next(1000000).ToString();
        return a;
    }
    private void initGvData()
    {
        ZSql zs = new ZSql();
        zs.Open("select *,(select StaffName from CS_Staff where StaffId=CS_ls.LS_Saleman) as  StaffName from CS_ls where LS_dh='" + hidsj.Value + "'");
        gvData.DataSource = zs.m_table;
        gvData.DataBind();
        zs.Close();
    }
    protected void btnAd_Click(object sender, EventArgs e)//添加商品临时数据
    {
        ZSql sale = new ZSql();
        ZSql sj = new ZSql();
        sj.Open("select * from CS_ls where LS_JZSX='"+Hidsj2.Value+"'" );
        if (sj.m_table.Rows.Count <= 0)
        {
            float salemoney = 0;
            float salepractical = 0;
            if (string.IsNullOrEmpty(txtCmoney.Value))
            {
                salemoney = float.Parse(HFjyjs.Value);
            }
            else
            {
                salemoney = float.Parse(txtCmoney.Value);//成交单价
                salepractical = float.Parse(txtpractical.Value);//实收单价
            }
            float tolmoney = float.Parse(txtNum.Value) * salemoney;
            float tolpractical = float.Parse(txtNum.Value) * salepractical;
            string sql = "insert into CS_ls(LS_dh,LS_band,LS_name,LS_type,LS_price,LS_num,LS_unit,LS_bs,LS_modul,LS_ProcMoney,LS_BarCode,LS_TotalMoney,LS_Warr,LS_JZSX,LS_typeStock,LS_practical,LS_TotalPractical,LS_Saleman)values('" + hidsj.Value + "','" + HidBrand.Value + "','" + HidName.Value + "','" + HFtype.Value + "'," + salemoney + "," + txtNum.Value + ",'" + HidUnit.Value + "','" + hidPH.Value + "','" + Text1.Value + "'," + HidProc.Value + ",'" + TextBox1.Value + "'," + tolmoney + "," + txtWarr.Text + ",'" + Hidsj2.Value + "','" + HFRKLX.Value + "'," + salepractical + "," + tolpractical + "," + ddlUser.SelectedValue.ToString() + ")";
            sale.Open(sql);
        }
        sale.Close();
        sj.Close();
        clearData();

        initGvData();
        initTable();
    }

    private void clearData()
    {
        Text1.Value = "";
        TextBox1.Value = "";
        HidName.Value = "";
        HidBrand.Value = "";
        txtCmoney.Value = "0";
        txtpractical.Value = "0";
        HidUnit.Value = "";
        hidPH.Value = "";
        txtNum.Value = "0";//初使化数量
    }
    protected void linkDel_Click(object sender, EventArgs e)//删除数据
    {
        string id = (sender as LinkButton).CommandArgument;
        ZSql del = new ZSql();
        del.Open("delete CS_ls where id=" + id);
        del.Close();
        initGvData();
        initTable();

    }
    private void initTable()
    {
        ZSql sel = new ZSql();

        sel.Open("select sum(LS_num)as num,sum(LS_TotalMoney)as price,sum(LS_TotalPractical)as TotalPractical from CS_ls where ls_dh='" + hidsj.Value + "' group by ls_dh ");
        if (sel.m_table.Rows.Count > 0)
        {
            lblXnum.Text = sel.m_table.Rows[0]["num"].ToString();
            lblXmoney.Text = sel.m_table.Rows[0]["price"].ToString().Substring(0, sel.m_table.Rows[0]["price"].ToString().Length - 2);
            lblHnum.Text = sel.m_table.Rows[0]["num"].ToString();
            lblHMoney.Text = sel.m_table.Rows[0]["TotalPractical"].ToString().Substring(0, sel.m_table.Rows[0]["TotalPractical"].ToString().Length - 2);
            HidCountNum.Value = sel.m_table.Rows[0]["num"].ToString(); //总数
            sel.Close();
        }
        else
        {
            lblXnum.Text = "0";
            lblXmoney.Text = "0";
            lblHnum.Text = "0";
            lblHMoney.Text = "0";
            HidCountNum.Value = "0";
        }
    }


    private void initDDL()//销售员数据
    {
        ZSql zs = new ZSql();
        zs.Open("select StaffId,StaffName from CS_Staff where AuditState=1 order by StaffName");
        ddlUser.DataSource = zs.m_table;
        ddlUser.DataTextField = "StaffName";
        ddlUser.DataValueField = "StaffId";
        ddlUser.DataBind();
        ddlUser.Items.Insert(0, new ListItem("选择销售员", ""));
        zs.Close();

    }
}
